#include <iostream>
#include <pthread.h>

int amount=0;
int const MAX=10;
int flag1= 0;
int flag2= 0;
	
	void primitiv1 (int & flag)
	{
		flag=1;
	}
	
	void primitiv2 (int & flag)
	{
		flag=0;
	}
	
	void* thread1(void *)
	{
		while(true)
		{
			while (flag1!=0);
			if (amount==1)
				primitiv1(flag2);	
			if (amount < MAX)
			{
				amount++;
				std::cout <<"prod "<<amount <<" "<< std::endl;
			}
			if (amount==MAX)			
				primitiv1(flag1);						
		}
	}
	
	void* thread2(void *)
	{
		while(true)
		{
			while (flag2!= 1);
			if (amount > 0)
			{
				amount--;
				std::cout << "cons "<<amount<<" "<< std::endl;
			}
			if (amount == MAX-1)			
				primitiv2(flag1);			
			if (amount==0)
				primitiv2(flag2);			
		}
	}

int main()
{
		pthread_t t1, t2;
		pthread_create (&t1, NULL, thread1, NULL);
		pthread_create (&t2, NULL, thread2, NULL);
		pthread_join (t1, NULL);
		pthread_join (t2, NULL);
		
		return 0;
}
